package com.aruba.controller;

import com.aruba.bean.Player;
import com.aruba.service.PlayerService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class PlayerController {
    // 文件存储位置
    private final static String FILESERVER = "http://localhost:8082/upload/";
    @Autowired
    private PlayerService playerService;

    @RequestMapping("registerPlayer.do")
    @ResponseBody
    public String register(Player player) {
        System.out.println(player);
        Integer rows = playerService.addPlayer(player);
        if (rows != null && rows > 0)
            return "success";

        return "failed";
    }

    @RequestMapping("findAllPlayer.do")
    @ResponseBody
    public List<Player> findAllPlayer() {
        List<Player> allPlayers = playerService.findAllPlayers();
        System.out.println(allPlayers);
        return allPlayers;
    }

    @RequestMapping("download.do")
    public void download(String photo, String filetype, HttpServletResponse response) throws IOException {
        // 设置响应头
        // 告诉浏览器要将数据保存到磁盘上,不在浏览器上直接解析
        response.setHeader("Content-Disposition", "attachment;filename="+photo);
        // 告诉浏览下载的文件类型
        response.setContentType(filetype);
        // 获取一个文件的输入流
        InputStream inputStream = new URL(photo).openStream();
        // 获取一个指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();
        // 向浏览器响应文件即可
        IOUtils.copy(inputStream, outputStream);
    }

    @RequestMapping("uploadImg.do")
    @ResponseBody
    public Map<String, Object> uploadImg(MultipartFile img, HttpServletRequest req) throws IOException {
        Map<String, Object> model = new HashMap<>();

        //将文件存放在部署的upload目录下
//        File savePathFile = new File(req.getServletContext().getRealPath("upload"));
//        System.out.println("存放路径：" + savePathFile.getAbsoluteFile());
//        if (!savePathFile.exists()) {
//            savePathFile.mkdirs();
//        }

        System.out.println("OriginalFilename：" + img.getOriginalFilename());
        String suffix = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf('.'));
        System.out.println("suffix：" + suffix);
        if (!suffix.equalsIgnoreCase(".jpg")) {
            model.put("msg", "文件类型必须为jpg");
            return model;
        }


        String saveFileName = UUID.randomUUID() + suffix;
//        //存放文件
//        File saveFile = new File(savePathFile, saveFileName);
//        img.transferTo(saveFile);

        // 创建sun公司提供的jersey包中的client对象
        Client client = Client.create();
        WebResource resource = client.resource(FILESERVER + saveFileName);
        // 将文件保存到另一个服务器
        resource.put(String.class, img.getBytes());

        model.put("msg", "上传成功");
//        model.put("filepath", "upload/" + saveFileName);
        model.put("filepath", FILESERVER + saveFileName);
        model.put("filetype", img.getContentType());

        System.out.println("返回结果：" + model);

        return model;
    }
}
